package com.example.chenzhiran.myapplication7.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.ResultSet;
import java.sql.Statement;

public class productPage {

    private Socket socket;
    private clientData indata;
    private serverData outdata = new serverData();
    ObjectOutputStream out;
    Statement statement;

    public productPage(Socket mysocket, clientData mydata, Statement stmt)
    {
        this.socket = mysocket;
        this.indata = mydata;
        this.statement = stmt;

        switch (indata.dataId)
        {
            case 0:
                sendPic(indata.inputmessage);
                break;
            case 1:
                like();
                break;
            case 2:
                canclelike();
                break;
        }

        try {
            out = new ObjectOutputStream(socket.getOutputStream());
            out.writeObject(outdata);
            out.close();
            System.out.printf("商品页数据已发送\n");
        }catch(Exception e)
        {
            e.printStackTrace();
        }


    }

    /*描述：发送一张商品图片
     *参数：空
     *返回：空
     */
    public void sendPic(String pic_path)
    {
        if(pic_path == null || pic_path.length() == 0)
        {
            outdata.datastatus = 1;
            return;
        }

        try {
            FileInputStream fin = new FileInputStream(new File(pic_path));
            byte[] pic = new byte[fin.available()];
            fin.read(pic);
            outdata.imgs.put(pic_path, pic);
            fin.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        char[] picpath = pic_path.toCharArray();
        char gender = picpath[10];
        String table;
        if(gender == 'm')
        {
            table = "malepic";
        }
        else table = "femalepic";

        String sql = "select * from " + table + " where loc = \"" + pic_path + "\"";
        try {
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                String path = rs.getString("loc");
                String describe = rs.getString("des");
                String sprice = rs.getString("pri");
                outdata.productdescribes.put(path,describe);
                outdata.productprices.put(path, Integer.parseInt(sprice));

                FileInputStream fin = new FileInputStream(new File(path));
                byte[] pic = new byte[fin.available()];
                fin.read(pic);
                outdata.imgs.put(path, pic);
                fin.close();

                outdata.datastatus = 0;
            }
            rs.close();
        }catch (Exception e)
        {
            outdata.datastatus = 1;
            e.printStackTrace();
        }

        sql = "select * from likelist where username = \"" + indata.username + "\"" + " and product = \"" + indata.inputmessage + "\"";
        int count = 0;
        try
        {
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next())
            {
                count += 1;
            }
            if(count != 0) outdata.islike = true;
            else outdata.islike = false;
            rs.close();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /*描述：点赞功能
     *参数：空
     *返回：空
     */
    public void like()
    {
        if(indata.inputmessage == null  || indata.inputmessage.length() == 0)
        {
            outdata.datastatus = 1;
            return;
        }
        else if(indata.username == null || indata.username.length() == 0)
        {
            outdata.datastatus = 1;
            return;
        }

        boolean finduser = false;
        try
        {
            String sql = "SELECT * FROM user";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next())
            {
                String name = rs.getString("username");
                if(name.equals(indata.username))
                {
                    finduser = true;
                    break;
                }
            }
            rs.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        if(finduser == false)
        {
            outdata.datastatus = 1;
            return;
        }

        int totalnum = 0;
        try
        {
            String sql = "select count(1) as totalCount from likelist where username = " + "\"" + indata.username + "\"" + " and product = " +  "\"" + indata.inputmessage + "\"";
            ResultSet numresult = statement.executeQuery(sql);
            while(numresult.next())
            {
                totalnum = numresult.getInt("totalCount");
            }
        }catch(Exception e)
        {
            outdata.datastatus = 1;
            e.printStackTrace();
        }

        if(totalnum != 0)
        {
            outdata.datastatus = 1;
            return;
        }

        try
        {
            String sql = "insert into likelist (username, product) value (" + "\"" + indata.username + "\"" + "," + "\"" + indata.inputmessage + "\"" + ")";
            int rs = statement.executeUpdate(sql);
            outdata.datastatus = 0;
        }catch(Exception e)
        {
            outdata.datastatus = 1;
            e.printStackTrace();
        }
    }

    /*描述：取消点赞
     *参数：空
     *返回：空
     */
    public void canclelike()
    {
        if(indata.inputmessage == null  || indata.inputmessage.length() == 0)
        {
            outdata.datastatus = 1;
            return;
        }
        else if(indata.username == null || indata.username.length() == 0)
        {
            outdata.datastatus = 1;
            return;
        }

        boolean finduser = false;
        try
        {
            String sql = "SELECT * FROM user";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next())
            {
                String name = rs.getString("username");
                if(name.equals(indata.username))
                {
                    finduser = true;
                    break;
                }
            }
            rs.close();
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        if(finduser == false)
        {
            outdata.datastatus = 1;
            return;
        }

        try
        {
            String sql = "delete from likelist where product = " + "\"" +  indata.inputmessage  + "\"" +  " and username = " +  "\"" +  indata.username  + "\"";
            int rs = statement.executeUpdate(sql);
            outdata.datastatus = 0;
        }catch(Exception e)
        {
            outdata.datastatus = 1;
            e.printStackTrace();
        }
    }
}
